package cn.project.controler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.project.model.User;
import cn.project.service.UserService;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	 String username=req.getParameter("username");
	 String password=req.getParameter("password");
		 
	 UserService userservice=new UserService();
	     User user =userservice.login(username, password);
	  
	     if(user!=null){
	    	 req.getSession().setAttribute("user", user);
	    	 req.getRequestDispatcher("MainServlet").forward(req, resp);
	     }else{
	    	 resp.sendRedirect("login.jsp");
	     }
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	doGet(req, resp);
	}

}
